Add self-healing settings file with corruption recovery#1591
Conversation
Fix startup crash when settings.json is corrupted (issue #1590). - Add SettingsJsonSanitizer utility that strips null bytes, fixes missing braces, and performs property-level JSON recovery - Wrap LoadSettings/LoadSettingsAsync deserialization in try-catch with progressive recovery: sanitize text -> property-level salvage -> fall back to defaults - Back up corrupted settings files to settings.json.bak before recovery - Make SaveSettings/SaveSettingsAsync atomic using temp-file-rename pattern to prevent partial writes that cause corruption - Add comprehensive tests for all recovery scenarios https://claude.ai/code/session_01Gej4ey7eUsbPRDqFWwxKyZ
|
CLA Assistant Lite bot CLA Assistant bot All Contributors have signed the CLA. |
There was a problem hiding this comment.
Code Review
This pull request introduces a SettingsJsonSanitizer service to handle corrupted settings files by stripping null bytes, fixing missing braces, and performing property-level recovery. Additionally, SettingsManager is updated to implement atomic file saves and automatic backups of corrupted settings. The review feedback highlights a potential issue with truncated JSON strings in the brace-fixing logic, notes that the property-level validation loop may be ineffective without schema checks, and recommends refactoring duplicated recovery logic in SettingsManager into a shared helper method.
- Replace manual byte-by-byte loop with Array.IndexOf + Array.FindAll
- Refactor TryFixBraces to use a Stack<char> for correct LIFO ordering
of mixed {} and [] bracket pairs
- Add test for mixed bracket truncation recovery
https://claude.ai/code/session_01Gej4ey7eUsbPRDqFWwxKyZ
- Close truncated string literals before appending missing brackets in TryFixBraces (fixes recovery of JSON cut mid-string) - Remove ineffective ToJsonString() validation loop since JsonNode.Parse already validates syntax; the typed deserializer handles type mismatches via Settings property initializer defaults - Extract duplicated recovery logic from LoadSettings/LoadSettingsAsync into shared DeserializeOrRecoverSettings helper - Add test for truncated-inside-string recovery https://claude.ai/code/session_01Gej4ey7eUsbPRDqFWwxKyZ
|
I have read the CLA Document and I hereby sign the CLA Generated by Claude Code |
|
recheck |
- Add timestamp to .bak filenames (e.g. settings.json.20260408-020219.bak) to preserve original backup if corruption recurs - Add try-catch around File.Move in SaveSettings/SaveSettingsAsync to clean up orphaned .tmp files if the move fails due to file locks https://claude.ai/code/session_01Gej4ey7eUsbPRDqFWwxKyZ
Fix startup crash when settings.json is corrupted (issue #1590).
missing braces, and performs property-level JSON recovery
with progressive recovery: sanitize text -> property-level salvage
-> fall back to defaults
pattern to prevent partial writes that cause corruption
https://claude.ai/code/session_01Gej4ey7eUsbPRDqFWwxKyZ